package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.TemperatureModel;
import view.CelsiusView;
import view.FahrenheitView;
import view.KelvinView;
import view.TemperatureView;


public class TemperatureController extends Controller {
	protected TemperatureModel modelT;
	protected TemperatureView viewT;
	
	public TemperatureController(int marginTop, int marginLeft, TemperatureModel model, TemperatureView view) {
		super(marginTop, marginLeft, model, view);
		modelT = model;
		viewT  = view; 
		this.viewT.initialize(model, marginLeft, marginTop);
		
		/** Binding a view-specific behavior **/
		if(view instanceof KelvinView){
			this.viewT.addRaiseTempListener(new RaiseTempListenerKelvin());
			this.viewT.addLowerTempListener(new LowerTempListenerKelvin());
			this.viewT.addDisplayListener(new DisplayListenerKelvin());
		}
		else if(view instanceof CelsiusView){
			this.viewT.addRaiseTempListener(new RaiseTempListenerCelsius());
			this.viewT.addLowerTempListener(new LowerTempListenerCelsius());
			this.viewT.addDisplayListener(new DisplayListenerCelsius());
		}
		else if(view instanceof FahrenheitView){
			this.viewT.addRaiseTempListener(new RaiseTempListenerFahrenheit());
			this.viewT.addLowerTempListener(new LowerTempListenerFahrenheit());
			this.viewT.addDisplayListener(new DisplayListenerFahrenheit());
		}
	}
	
	/** Celsius related actions **/
    class RaiseTempListenerCelsius implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			modelT.setC(modelT.getC() + 1.0);
		}
	}

	class LowerTempListenerCelsius implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			modelT.setC(modelT.getC() - 1.0);
		}
	}

	class DisplayListenerCelsius implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			double value = viewT.getDisplay();
			modelT.setC(value);
		}
	}
	
	/** Kelvin related actions **/
	class RaiseTempListenerKelvin implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			modelT.setK(modelT.getK() + 1.0);
		}
	}

	class LowerTempListenerKelvin implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			modelT.setK(modelT.getK() - 1.0);
		}
	}

	class DisplayListenerKelvin implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			double value = viewT.getDisplay();
			modelT.setK(value);
		}
	}
	
	
	/** Fahrenheit related actions **/
	class RaiseTempListenerFahrenheit implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			modelT.setF(modelT.getF() + 1.0);
		}
	}

	class LowerTempListenerFahrenheit implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			modelT.setF(modelT.getF() - 1.0);
		}
	}

	class DisplayListenerFahrenheit implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			double value = viewT.getDisplay();
			modelT.setF(value);
		}
	}	
	
}
